function [] = compare_blizzard()
% function [] = compare_blizzard()
% 
% Plots the Blizzard TTS comparison from
%
% F. Ribeiro, D. Florencio, C. Zhang and M. Seltzer, "crowdMOS: An Approach for
% Crowdsourcing Mean Opinion Score Studies", submitted to ICASSP 2011.

!cp blizzard_crowdmos1_hp/* .
[algorithms, mos_cmos1hp, ci95_cmos1hp] = compute_mos_ci95_3gaussian();

!cp blizzard_crowdmos2_hp/* .
[algorithms, mos_cmos2hp, ci95_cmos2hp] = compute_mos_ci95_3gaussian();

!cp blizzard_crowdmos2_ls/* .
[algorithms, mos_cmos2ls, ci95_cmos2ls] = compute_mos_ci95_3gaussian();

!cp blizzard_paid_participants/* .
[algorithms, mos_bliz_paid, ci95_bliz_paid] = compute_mos_ci95_3gaussian();

!cp blizzard_online_volunteers/* .
[algorithms, mos_bliz_onlinev, ci95_bliz_onlinev] = compute_mos_ci95_3gaussian();

!rm *txt

[dummy,idx] = sort(mos_cmos1hp,2,'descend');
algorithms = algorithms(idx);

mos_cmos1hp  = mos_cmos1hp(idx);
ci95_cmos1hp = ci95_cmos1hp(idx);

mos_cmos2hp  = mos_cmos2hp(idx);
ci95_cmos2hp = ci95_cmos2hp(idx);

mos_cmos2ls  = mos_cmos2ls(idx);
ci95_cmos2ls = ci95_cmos2ls(idx);

mos_bliz_paid  = mos_bliz_paid(idx);
ci95_bliz_paid = ci95_bliz_paid(idx);

mos_bliz_onlinev  = mos_bliz_onlinev(idx);
ci95_bliz_onlinev = ci95_bliz_onlinev(idx);

figure
algorithms{1} = 'REF';
plot_mos_ci95(algorithms, ...
	      [ mos_cmos1hp  ; mos_cmos2hp  ; mos_cmos2ls  ; mos_bliz_paid  ; mos_bliz_onlinev  ]', ...
	      [ ci95_cmos1hp ; ci95_cmos2hp ; ci95_cmos2ls ; ci95_bliz_paid ; ci95_bliz_onlinev ]')
legend('crowdMOS 1 (headphones)', 'crowdMOS 2 (headphones)', 'crowdMOS 2 (loudspeakers)', 'Blizzard Paid Participants', 'Blizzard Online Volunteers')

print -depsc blizzard_comparison.eps

%%%%

fprintf('r between cmos1hp and bliz_paid: %.2f\n', corr_coeff(mos_cmos1hp, mos_bliz_paid));
fprintf('r between cmos2hp and bliz_paid: %.2f\n', corr_coeff(mos_cmos2hp, mos_bliz_paid));
fprintf('r between cmos2ls and bliz_paid: %.2f\n', corr_coeff(mos_cmos2ls, mos_bliz_paid));
fprintf('r between bliz_onlinev and bliz_paid: %.2f\n', corr_coeff(mos_bliz_onlinev, mos_bliz_paid));

%%%%

function [] = plot_mos_ci95(algorithms, values, ci95)
ngroups = size(values, 1);
nbars   = size(values, 2);

assert(ngroups > 1);

hold on
linewidth = 0.5;

h = bar(values, 'group', 'LineWidth', linewidth);
for i = 1:nbars
	x = get(get(h(i), 'children'),'xdata');
	x = mean(x([1 3],:));		% x coordinates for bar i of each group
	h_eb = errorbar(x, values(:,i), ci95(:,i), ci95(:,i), 'k', 'LineStyle', 'none', 'LineWidth', linewidth);
	errorbar_tick(h_eb, 150);
end

set(gca, 'XTick', 1:ngroups)
set(gca, 'XTickLabel', algorithms)
set(gca, 'FontSize', 5.5)

xlabel('Blizzard 2009 Algorithm')
ylabel('MOS')
V = axis; V([3 4]) = [1 5]; axis(V);
pbaspect([1 0.20 1]);

%%%%

function r = corr_coeff(x,y)
x = x(:);
y = y(:);

Sxx  = norm(x - mean(x));
Syy  = norm(y - mean(y));
Sxy2 = (x - mean(x))' * (y - mean(y));

r = Sxy2 / (Sxx * Syy);

